﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace Sequence
{
    public partial class FrmGetParam : Form
    {
        public string sName = "";
        public string sValue = "";
        DbConnection m_oConnection;

        public FrmGetParam(DbConnection oConnection)
        {
            InitializeComponent();
            m_oConnection = oConnection;
        }

        private DbConnection GetConntion()
        {
            return m_oConnection;
        }

        public FrmGetParam(DbConnection oConnection,string sProcessId,string sStepId, string sType)
        {
            InitializeComponent();
            m_oConnection = oConnection;
            String sSql = "";


            sSql = @"
select t_api_qd_process_step.name || '>' || t_api_qd_process_step_params.name as value,t_api_qd_process_step_params.name 
from t_api_qd_process_step_params 
join t_api_qd_process_step on t_api_qd_process_step_params.process_id = t_api_qd_process_step.process_id 
                          and t_api_qd_process_step_params.step_id = t_api_qd_process_step.id 
where 
    t_api_qd_process_step_params.params_type='O' 
    and t_api_qd_process_step_params.process_id=$sProcessId$ 
    and t_api_qd_process_step_params.step_id<$sStepId$ ";

            sSql = sSql.Replace("sProcessId", sProcessId);
            sSql = sSql.Replace("sStepId", sStepId);

            try
            {
                DbCommand command = GetConntion().CreateCommand();
                command.CommandText = sSql;
                command.CommandType = CommandType.Text;
                DbDataReader oDataReader = command.ExecuteReader();

                while (oDataReader.Read())
                {
                    ListViewItem oListViewItem = listView1.Items.Add(oDataReader["NAME"].ToString());
                    oListViewItem.SubItems[0].Text = oDataReader["NAME"].ToString();
                    oListViewItem.SubItems.Add(oDataReader["VALUE"].ToString());                               
                }
                ListViewItem oListViewItem2 = listView1.Items.Add("ToDo");
                oListViewItem2.SubItems[0].Text = "ToDo";
                oListViewItem2.SubItems.Add("?");

                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                DialogResult dialogResult = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        public FrmGetParam(DbConnection oConnection, string sSql)
        {
            InitializeComponent();
            m_oConnection = oConnection;

            try
            {
                DbCommand command = GetConntion().CreateCommand();
                command.CommandText = sSql;
                command.CommandType = CommandType.Text;
                DbDataReader oDataReader = command.ExecuteReader();

                while (oDataReader.Read())
                {
                    ListViewItem oListViewItem = listView1.Items.Add(oDataReader["NAME"].ToString());
                    oListViewItem.SubItems[0].Text = oDataReader["NAME"].ToString();
                    oListViewItem.SubItems.Add(oDataReader["VALUE"].ToString());
                }
                

                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                DialogResult dialogResult = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void FrmGetParam_Load(object sender, EventArgs e)
        {

        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                sName = listView1.SelectedItems[0].Text;
                sValue = listView1.SelectedItems[0].SubItems[1].Text;                   
            }

            this.Close();
        }
    }
}
